<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<%
    String path = request.getContextPath();
			String basePath = request.getScheme() + "://"
					+ request.getServerName() + ":" + request.getServerPort()
					+ path + "/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<title>AFC设备维修系统-用户信息编辑</title>
		<style>
		#organizesTree {
			margin-top: 10px;
			border: 1px solid #617775;
			background: #f0f6e4;
			min-width: 180px;
			max-height: 400px;
			overflow-y: scroll;
			overflow-x: auto;
		}
		</style>
	</head>

	<body>
		<jsp:include page="/jsp/common/top.jsp"></jsp:include>
		<!-- 顶部start -->
		<div id="main">
			<div class="body">
				<!-- 导航菜单 -->
				<jsp:include page="/jsp/common/nav.jsp"></jsp:include>
				<!-- 主体 -->
			<form action="doUserInfoEdit.action" method="post" id="addFrm" >
				<div class="main_body">
					<s:if test="%{null == userInfo}">
						<table class="condition" style="width:100%; font-size:14px; font-weight:bold; color:#000000;" align="center">
							<tr>
							<td>
								<input type="button" class="btn-title" value="返回" onclick="javascript:window.location.href='userInfoList.action';" />
							</td>
							</tr>
						</table>
					</s:if>
					<div id="add_div">
						<table class="table_add" style="width: 100%;">
						<s:if test="%{null == userInfo}">
							<tr>
								<th style="width: 150px; text-align: center;" colspan="3">
									<font color="red"> 该条记录不存在或已删除！</font>								
								</th>
							</tr>
							</s:if>
							<s:else>
							<tr>
								<th style="width: 150px;">
									用户账号：
								</th>
								<td style="width: 225px;">
									<input type="hidden" name="userInfo.id" value="${userInfo.id}"/>
									<input id="code" type="text" class="text" style="width: 225px;"
									  onkeydown="if(event.keyCode==8)return false;"  
									name="userInfo.userCode" readonly="readonly" value="${userInfo.userCode }" />
									<font color="red" id="fontId" >&nbsp;&nbsp;*</font>
								</td>
								<th style="width: 189px;">
									用户名：
								</th>
								<td>
									<input id="name" type="text" class="text" style="width: 225px;" name="userInfo.userName"  value="${userInfo.userName }" />
									<input id="hname" type="hidden" value="${userInfo.userName }" />
									<font color="red" >&nbsp;&nbsp;*</font>
								</td>
							</tr>
							<tr>
								<th style="width: 150px;">
									用户密码：
								</th>
								<td style="width: 225px;">
									<input type="password" id="pwd" class="text" style="width: 225px;" name="userInfo.password"  value="${ userInfo.password}" />
									<font color="red" >&nbsp;&nbsp;*</font>
								</td>
								<th style="width: 150px;">
									邮箱：
								</th>
								<td style="width: 225px;">
									<input type="text" id="email" class="text" style="width: 225px;" name="userInfo.email"  value="${userInfo.email }" />
									<font color="red" >&nbsp;&nbsp;*</font>
								</td>
							</tr>
							<tr>
								<th style="width: 150px;">
									角色：
								</th>
								<td style="width: 225px;">
									<input type="hidden" id="roleID" name="userInfo.roleCode"  value="${userInfo.roleCode }" />
									<input name="text2" type="text" class="text" id="showAddRole"
									 onkeydown="if(event.keyCode==8)return false; "
										style="width: 225px;"  value="${userInfo.roleName}"  readonly />
										<font color="red" >&nbsp;&nbsp;*</font>
								</td>
								<th style="width: 189px;">
									所属组织：
								</th>
								<td style="width: 225px;">
									<input type="hidden"  id="orgID" name="userInfo.blongOrgCode"  value="${userInfo.blongOrgCode }" />
									<input name="text2" type="text" class="text" id="showOrganize"
										style="width: 225px;" onkeydown="if(event.keyCode==8)return false;"
										onclick="showMenu('showOrganize', 'organizeContent');" value="${userInfo.blongOrgName }" readonly/>
										<font color="red" >&nbsp;&nbsp;*</font>
								</td>
							</tr>
							
							<tr>
								<th style="width: 150px;">
									用户号码：
								</th>
								<td style="width: 225px;">
									<input type="text" class="text" style="width: 225px;" id="phone" name="userInfo.telephoneNum" value="${userInfo.telephoneNum}" />
									<font color="red" >&nbsp;&nbsp;*</font>
								</td>
								<th style="width: 189px;">
									&nbsp;
								</th>
								<td style="width: 225px;">
									&nbsp;
								</td>
							</tr>
 						</s:else>
						</table>
					</div>
					
					<div style="margin: 8px 0 0 150px;">
						<s:if test="%{null != userInfo}">
							<input type="button" id="saveBtn" class="btn-title" value="保存"/>
							&nbsp;&nbsp;	
							<input type="button" class="btn-title" value="取消" onclick="javascript:doCancel('userInfoList.action');"/>
						</s:if>
					</div>
				</div>
				</form>
			</div>
		</div>

		<div id="organizeContent" class="menuContent"
			style="display: none; position: absolute;">
			<ul id="organizesTree" class="ztree" style="margin-top: 0;"></ul>
		</div>
		
		<div id="roleAddDialog" title="角色列表" style="display: none;">
			<table width="0" border="0" cellspacing="0" cellpadding="0">
				<tr>
					<td>
						<s:select id="roleName" size="20"
							cssStyle="width: 160px; height: 240px;" list="roleLists" name="flag"
							listKey="rolecode" listValue="rolename" ondblclick="addRoleDBlclick();">
						</s:select>
					</td>
				</tr>
			</table>
		</div>
		<!-- 顶部end -->
		<jsp:include page="/jsp/common/bottom.jsp"></jsp:include>
		<div id="tempDiv"></div>
<script type="text/javascript">
	
	//获取后台数据显示组织架构
	var organizes =${flag};
	
	//组织架构初始化
	var organizeSetting = {
		view : {
			dblClickExpand : false
		},
		data : {
			simpleData : {
				enable : true
			}
		},
		callback : {
			beforeClick : beforeClick,
			onClick : organizeOnClick
		}
	};

	//点击事件
	function beforeClick(treeId, treeNode) {
		var check = (treeNode && !treeNode.isParent);
		if (!check)
			alert("请选择下级节点!");
		return check;
	}

	//组织架构点击事件
	function organizeOnClick(e, treeId, treeNode) {
		var zTree = $.fn.zTree.getZTreeObj("organizesTree"), nodes = zTree.getSelectedNodes(), v = "";
		nodes.sort(function compare(a, b) {
			return a.id - b.id;
		});
		for ( var i = 0, l = nodes.length; i < l; i++) {
			v += nodes[i].name + ",";
		}
		if (v.length > 0)
			v = v.substring(0, v.length - 1);
		var orgNameObj = $("#showOrganize");
		orgNameObj.attr("value", v);
		//设置隐藏域中组织编号  
		$('#orgID').val(treeNode.id);
		hideMenu('organizeContent');
	}

	//隐藏组织架构
	function hideMenu(divId) {
		$("#" + divId).fadeOut("fast");
		$("body").unbind("mousedown", {
			tempDiv : divId
		}, onBodyDown);
	}

	//组织架构弹出窗
	function showMenu(objId, divId) {
		var cityObj = $("#" + objId);
		var cityOffset = $("#" + objId).position();
		$("#" + divId).css( {
			left : cityOffset.left + "px",
			top : cityOffset.top + cityObj.outerHeight() + "px"
		}).slideDown("fast");
		$("body").bind("mousedown", {
			tempDiv : divId
		}, onBodyDown);
	}

	//页面加载
	$(function() {
		//设置组织架构弹出框
		$.fn.zTree.init($("#organizesTree"), organizeSetting, organizes);

		//点击事件
		$('#showAddRole').click(function() {
			$('#roleAddDialog').dialog('open');
			return false;
		});

		// 角色弹窗初始化
		$('#roleAddDialog').dialog( {
			autoOpen : false,
			modal: true,
			width : 200,
			height : 300,
			position : [ 547, 265 ],
			show: "blind",
			hide:"slide"
		//显示位置
		});
		
		//验证用户名称是否存在
		$('#name').blur(function(){
			var hName =$.trim($('#hname').val());
			var item = $.trim($(this).val());
			var myReg = /^[^@\/\'\\\"#$%&\^\*<>#+]+$/;
			if (null != item && item != '') 
			{
				if(hName !=item){
					//特殊字符校验
					if (!myReg.test(item)) {
						alert("用户名称不能含有$、#、%、/、*、<、>、'、\"、\\、&、+等特殊字符，请重新输入！");
						$('#name').focus();
						return false;
					}else
					{
						var url = encodeURI(encodeURI('isExistUserName.action?userInfo.userName=' + item));
						$.ajax( {
							url : url,
							type : "POST",
							cache : false,
							success : function(message) {
								if (message == "true") 
								{
									alert("用户名称已存在，请重新输入！");
									$('#name').focus();
								}else if(message=="error"){
									window.location.href="goError.action";
								}
							}
						});
					}
				}
			}
		});
		//保存按钮事件
		$('#saveBtn').click(function(){
			if(checkFrm()){
				var code =$('#pwd').val();
				code= code.replace(/\+/g, "%2B"); 
				$('#pwd').val(code);
				
				$('#addFrm').ajaxSubmit( { 
					target:'#tempDiv',    
				    url:'doUserInfoEdit.action', 
				    type:"POST",
					contentType:"application/x-www-form-urlencoded;charset=UTF-8",
				    error : function() {   
				    	alert('服务器异常！');
				    	$('#tempDiv').html('');
				    	window.location.href="userInfoList.action";
				    },
				    success: function(data){
			   			if(data=="error"){
							window.location.href="goError.action";
						}else{
					    	$('#tempDiv').html('');
					    	window.location.href="userInfoList.action";
					    	alert('用户编辑成功！');
				    	}
				    } 
				});   
			}
		});
	});

	// 角色弹窗显示
	$('#showAddRole').click(function() {
		$('#roleAddDialog').dialog('open');
		return false;
	});

	//鼠标点击别的地方隐藏弹出属性菜单
	function onBodyDown(event) {
		var divId = event.data.tempDiv;
		if (!(event.target.id == "showOrganize" || $(event.target).parents(
				"#" + divId).length > 0)) {
			hideMenu(divId);
		}
	}
	//角色双击事件
	function addRoleDBlclick() {
		//获取当前option名称
		var name = document.getElementById("roleName").options[window.document.getElementById("roleName").selectedIndex].text;
		$("#showAddRole").val(name);
		
		//id 赋值隐藏域
		$("#roleID").val($("#roleName").val());
		$("#roleAddDialog").dialog("close");
	}

	//执行提交
	function doSubmit(url) {
		document.forms[0].action = url;
		document.forms[0].submit();
	}
	
	//表单验证
	function checkFrm()
	{
		//正则表达式
		var myReg = /^[^@\/\'\\\"#$%&\^\*<>+]+$/;
		
		//用户账号验证
		if(!paramIsNull('code'))
		{	
			alert('用户账号不能为空！');
			return false;
		}
		else
		{
			var  code =$('#code').val();
			if(getLen(code)>30)
			{
				alert("用户账号字符个数不能大于30！");
				$('#code').focus();
				return false;
			}
			//特殊字符校验
			if (!myReg.test(code)) 
			{
				alert("用户账号不能含有$、#、%、/、*、<、>、'、\"、\\、&、+等特殊字符，请重新输入！");
				$('#code').focus();
				return false;
			}
		}
		
		//用户名验证  name
		if(!paramIsNull('name'))
		{	
			alert('用户名称不能为空！');
			return false;
		}
		else
		{
			var code =$('#name').val();
			if(getLen(code)>30)
			{
				alert("用户名称字符个数不能大于30,中文按3个字符计算！");
				$('#name').focus();
				return false;
			}
			//特殊字符校验
			if (!myReg.test(code)) {
				alert("用户名称不能含有$、#、%、/、*、<、>、'、\"、\\、&、+等特殊字符，请重新输入！");
				$('#name').focus();
				return false;
			}
		}
		
		//用户名验证  name
		if(!paramIsNull('pwd'))
		{	
			alert('用户密码不能为空！');
			return false;
		}
		else
		{
			var code =$('#pwd').val();
			if(code.length <6 || code.length > 20)
			{
				alert("请输入6-20 为长度字符的密码！");
				$('#pwd').focus();
				return false;
			}
		}
		
		//邮箱验证  email
		if(!paramIsNull('email'))
		{	
			alert('用户邮箱不能为空！');
			return false;
		}
		else
		{
		  	var myEmail = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
			var code =$('#email').val();
			if(code.length>30 || !myEmail.test(code)){
				alert("请输入有效E-mail！");
				$('#email').focus();
				return false;
			}
		}
		//角色  showAddRole
		if(!paramIsNull('showAddRole'))
		{	
			alert('请选择用户角色！');
			return false;
		}
		
		//组织架构 showOrganize
		if(!paramIsNull('showOrganize'))
		{	
			alert('请选择用户所属组织架构！');
			return false;
		}
		
		//手机号码验证 phone
		if(!paramIsNull('phone'))
		{	
			alert('用户密码不能为空！');
			return false;
		}
		else
		{
			var code =$('#phone').val();
			if(code.length!=11 || !(/^[0-9]{11}$/).test(code))
			{
				alert("请输入有效的11位手机号码！");
				$('#phone').focus();
				return false;
			} 
		}
		return true;
	}
	
	//参数非空验证
	function paramIsNull(paramId){
		var temp = $('#'+paramId).val();
		if(null == temp || temp=='')
		{
			return false;
		}
		else{
			//文本框去左右空格重新赋值
			$('#'+paramId).val($.trim(temp));
			return true;
		}
	} 
	
	/**计算字符串长度  中文三个字符计算**/
	function getLen(str){
	   var len=0;
	   if(null!=str&&str!=""){
	   for(var i=0;i<str.length;i++){
	   var intCode=str.charCodeAt(i);
	   //如果ASCII码等于34(")38(&)39(')47(/)60(<)62(>)92(\)也算三个字符
	   if (intCode>=0&&intCode<=127&&intCode!=34&&intCode!=39&&intCode!=47&&intCode!=60&&intCode!=62&&intCode!=92){
	        len=len+1;
	      }else{
	         len=len+3;
	            }
	       }
	    }
	   return len;
	}
		/**取消按钮事件*/
	function doCancel(url){
		if(confirm('您确认取消本次操作？')){
			window.location.href=url;
		}
	}
	
</script>
	</body>
</html>
